<?php
/**
 * @page range_list_module Range List Module
 * 
 * Like @ref filter_module, this module can output filter options for numeric fields, say, price,
 * or any other numeric field for filtering the list.
 * 
 * @image html range_list_module.jpg
 * 
 */
class RangeListModule extends GigaModule
{
	var $name = 'Range List';
	
	var $pageProperties = array(
		'before' => '',
		'after' => ''
	);
	
	var $configuration = null;

	function __construct()
	{
		$this->configuration = array(
		'before_text' => array(
			'type' => 'text',
			'label' => "Before Text (optional)",
			'tooltip' => 'Text to appear on top of module, before the range'
		), 
		'category' => array(
			'type' => 'select', 
			'empty' => __('-Select-'),
			'label' => 'Select Category', 
			'options' => array(),
			'triggers' => array('onChange' => array('update' => 'range_field', 'from_model' => 'RangeOption')),
			'validate'=>VALID_NOT_EMPTY,
		),
		'range_field' => array(
			'type' => 'select',
			'empty' => __('-Select-'),
			'label' => "Range Field",
			'tooltip' => 'This is a list of your numeric fields which can be divided among ranges.',
			'validate'=>VALID_NOT_EMPTY,
		),
		'ranges' => array(
			'type' => 'parent', 
			'childInfo' => array(
				'model' => 'RangeOption',
				'child_group' => 'add',
				'allowMultiple' => true
			),
			'ignoreCallback' => true,
			'label' => 'Specify Range'
		),
		'prefix' => array(
			'type' => 'text',
			'label' => "Prefix (optional)",
			'tooltip' => 'Text to show before every range amount'
		), 
		'suffix' => array(
			'type' => 'text',
			'label' => "Suffix (optional)",
			'tooltip' => 'Text to show after every range amount'
		), 
		'separator' => array(
			'type' => 'text',
			'label' => "Separator",
			'tooltip' => 'Text which appears between two ranges, (ex: 10 - 20, "-" is a separator here. )'
		),
		'after_text' => array(
			'type' => 'text',
			'label' => "After Text (optional)",
			'tooltip' => 'Text to appear at bottom of module, after the range'
		)
	);
		$categories = cri('ListCategory')->getOptions();
		$this->configuration['category']['options'] = $categories;
		
		$this->pageProperties['before'] = '<link media="all" rel="stylesheet" type="text/css" href="'.cr('cmsConfig.Backend.siteUrl').'/cms/horizontalInput/2.css"  /> ';
	}
	
	function render($params)
	{
		//prd($params);
		App::import('Vendore','Searchable.GigaSearch');
		$before_text = null;
		extract($params);
		$appHtml = ClassRegistry::getObject('view')->loaded['appHtml'];
		$output = "";
		if($before_text)
		{
			$output .= $before_text.'<br /><br />';
		}
		$output .= '<ul class="styled_list">';
		$ListingModel = cri('Listing');
		$GigaSearchObject = new GigaSearch($ListingModel);
		foreach($ranges as $range)
		{
			$searchData = array();
			if(strpos($range['min_value'],'+') === false)
			{
				$link = $prefix . $range['min_value'] . $suffix. $separator . $prefix . $range['max_value'] . $suffix;
				$seachData[$range_field] = array('opreator'=>GigaSearch::OP_BETWEEN,'min_value'=>$range['min_value'],'max_value'=>$range['max_value']);
			}else
			{
				$min = str_replace('+','',$range['min_value']);
				$seachData[$range_field] = array('opreator'=>GigaSearch::OP_GREATER_THAN,'min_value'=>$min);
				$link = $prefix . $range['min_value'].$suffix;				
			}			
			if(strpos($range['max_value'],'+') ===false)
			{
				
			}
			else
			{
				$min = str_replace('+','',$range['max_value']);
				$seachData[$range_field] = array('opreator'=>GigaSearch::OP_LESS_THAN,'max_value'=>$min);
				$link = $prefix . $range['max_value'].$suffix;				
			}
			
			$urlParams = $GigaSearchObject->toUrlArray($seachData);
			$url = am(array('controller'=>"listings",'plugin'=>false,'action'=>"index","advancedSearch",$category),$urlParams);
			$output .= "<li>".$appHtml->link($link, $url) ." </li>";
		}
		
		$output .= "</ul>";
		if($after_text)
		{
			$output .= '<br />'.$after_text; 
		}
		return $output;
	}
}
?>
